#!./common/bats/bin/bats 
# -*-sh-*-

source ./common/test_helper_functions.bash || exit 1
source ./common/functions || exit 1

if [ -s ./common/TEST_ENV ];then
    source ./common/TEST_ENV
fi

check_rms

NODES=2
TASKS=8
ARGS=8

PKG=MPIP
module=mpiP
library=libmpiP
testname=perftools/mpiP
rpm=mpiP-$LMOD_FAMILY_COMPILER-$LMOD_FAMILY_MPI${DELIM}

@test "[$testname] Verify $PKG module is loaded and matches rpm version ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    module list $module | grep "1) $module" >& .cmd_output || exit 1
    run grep $module .cmd_output 
    assert_success
    
    # check version against rpm
    local version="$(rpm -q --queryformat='%{VERSION}\n' $rpm)"
    run cat .cmd_output
    assert_output "  1) $module/$version"
}

@test "[$testname] Verify module ${PKG}_DIR is defined and exists ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    DIR=${PKG}_DIR
    if [ -z ${!DIR} ];then
        flunk "${PKG}_DIR directory not defined"
    fi
    
    if [ ! -d ${!DIR} || -z "${!DIR}" ];then
        flunk "directory ${!DIR} does not exist"
    fi 
}

# ----------
# Lib Tests
# ----------

@test "[$testname] Verify module ${PKG}_LIB is defined and exists ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    LIB=${PKG}_LIB

    if [ -z ${!LIB} ];then
        flunk "${PKG}_LIB directory not defined"
    fi
 
    if [ ! -d ${!LIB} ];then
        flunk "directory ${!LIB} does not exist"
    fi 
}

@test "[$testname] Verify static library is not present in ${PKG}_LIB ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    LIB=${PKG}_LIB

    if [ -z ${!LIB} ];then
        flunk "${PKG}_LIB directory not defined"
    fi

    if [ -e ${!LIB}/${library}.a ];then
        flunk "${library}.a exists but not expecting it"
    fi 
}

@test "[$testname] Verify dynamic library is available in ${PKG}_LIB ($LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    LIB=${PKG}_LIB

    if [ -z ${!LIB} ];then
        flunk "${PKG}_LIB directory not defined"
    fi

    if [ ! -e ${!LIB}/${library}.so ];then
        flunk "${library}.so does not exist when expecting it"
    fi 
}

@test "[$testname] C   binary generates profile data on two nodes under resource manager ($RMS/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s C_test ];then
	flunk "C_test binary does not exist"
    fi

    rm -f *.mpiP

    run run_mpi_binary ./C_test $ARGS $NODES $TASKS
    assert_success

    # Verify an mpiP file was generated

    run ls C_test.*.mpiP
    assert_success

    # Verify that callsite info is correct

    grep -A3 Callsites *.mpiP | grep Allreduce | grep -q C_test.c 
    if [ $? -ne 0 ];then
        flunk "Allreduce callsite not detected"
    fi
}

@test "[$testname] C++ binary generates profile data on two nodes under resource manager ($RMS/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s CXX_test ];then
	flunk "CXX_test binary does not exist"
    fi

    rm -f *.mpiP

    run run_mpi_binary ./CXX_test $ARGS $NODES $TASKS
    assert_success

    # Verify an mpiP file was generated

    run ls CXX_test.*.mpiP
    assert_success

    # Verify that callsite info is correct

    grep -A3 Callsites *.mpiP | grep Allreduce | grep -q CXX_test.c 
    if [ $? -ne 0 ];then
        flunk "Allreduce callsite not detected"
    fi
}

@test "[$testname] F90 binary generates profile data on two nodes under resource manager ($RMS/$LMOD_FAMILY_COMPILER/$LMOD_FAMILY_MPI)" {
    if [ ! -s CXX_test ];then
	flunk "F90_test binary does not exist"
    fi

    rm -f *.mpiP

    run run_mpi_binary ./F90_test $ARGS $NODES $TASKS
    assert_success

    # Verify an mpiP file was generated

    run ls F90_test.*.mpiP
    assert_success

    # Verify that callsite info is correct

    grep -A3 Callsites *.mpiP | grep Allreduce | grep -q F90_test.f90
    if [ $? -ne 0 ];then
        flunk "Allreduce callsite not detected"
    fi
}



rm -f .cmd_output
